﻿using System;
using System.Drawing;
using System.Windows.Forms;
using SkaldRogueLike.Main.Dungeon;

namespace SkaldRogueLike.GUI
{
    public partial class Form1 : Form
    {
        private const int XStep = 20;
        private const int YStep = 20;
        private const int XOffset = 20;
        private const int YOffset = 20;
        private readonly Generator _generator = new Generator();
        private Dungeon _dungeon;

        public Form1()
        {
            InitializeComponent();
            Paint += Form1_Paint;
            Click += Form1_Click;
            _dungeon = _generator.GenerateMap(25, 18, 0, 0);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            var g = e.Graphics;

            foreach (var location in _dungeon.CorridorCellLocations)
            {
                var cell = _dungeon[location];

                g.FillRectangle(GetWallCount(cell) == 4 ? Brushes.Black : Brushes.Transparent, TopLeft(location).X, TopLeft(location).Y, XStep, YStep);

                var pen = GetSidePen(cell.GetSide(Dir.North));
                g.DrawLine(pen, TopLeft(location), TopRight(location));
                if (cell.GetSide(Dir.North) == Side.Door)
                    g.DrawRectangle(
                        pen, TopLeft(location).X + 1 + (XStep - 2) / 4,
                        TopLeft(location).Y - 3, (XStep / 2) - 1, 6);

                pen = GetSidePen(cell.GetSide(Dir.West));
                g.DrawLine(pen, TopLeft(location), BottomLeft(location));
                if (cell.GetSide(Dir.West) == Side.Door)
                    g.DrawRectangle(
                        pen, TopLeft(location).X - 3,
                        TopLeft(location).Y + 1 + (YStep - 2) / 4, 6, (XStep / 2) - 1);

                g.DrawLine(GetSidePen(cell.GetSide(Dir.South)), BottomLeft(location), BottomRight(location));
                g.DrawLine(GetSidePen(cell.GetSide(Dir.East)), TopRight(location), BottomRight(location));
            }
        }

        private static int GetWallCount(Cell cell)
        {
            var wallCount = 0;
            if (cell.GetSide(Dir.North) == Side.Wall) wallCount++;
            if (cell.GetSide(Dir.South) == Side.Wall) wallCount++;
            if (cell.GetSide(Dir.West) == Side.Wall) wallCount++;
            if (cell.GetSide(Dir.East) == Side.Wall) wallCount++;
            return wallCount;
        }

        private static Point TopLeft(Point location)
        {
            return new Point(XOffset + (location.X * XStep), YOffset + (location.Y * YStep));
        }

        private static Point BottomLeft(Point location)
        {
            return new Point(XOffset + (location.X * XStep), YOffset + (location.Y * YStep) + YStep);
        }

        private static Point TopRight(Point location)
        {
            return new Point(XOffset + (location.X * XStep) + XStep, YOffset + (location.Y * YStep));
        }

        private static Point BottomRight(Point location)
        {
            return new Point(XOffset + (location.X * XStep) + XStep, YOffset + (location.Y * YStep) + YStep);
        }

        private static Pen GetSidePen(Side side)
        {
            switch (side)
            {
                case Side.Wall:
                case Side.Empty:
                    return new Pen(Color.Transparent);
                default:
                    return new Pen(Color.Red);
            }
        }

        private void Form1_Click(object sender, EventArgs e)
        {
            _dungeon = _generator.GenerateMap(25, 18, 0, 0);
            Invalidate();
        }
    }
}
